/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import model.User;

/**
 *
 * @author Rima
 */
public class databaseAdmin extends DBClass {

    private String sql;
    private User dataUser;
    private Connection con;
    private Statement st;
    private ResultSet rs;

    public User cekLogin(String user, String password) throws ClassNotFoundException, SQLException {
        User dataUser = new User();
        con = buatKoneksi();
        st = con.createStatement();
        rs = st.executeQuery("SELECT * FROM `admin` WHERE email = '" + user + "' and password = '" + password + "'");
        if (rs.next()) {
            dataUser.setNama(rs.getString(2));
            dataUser.setRole(rs.getInt(4));
            dataUser.setId_ruang(rs.getInt(5));
            return dataUser;
//                String Sts = rs.getInt(4);
//                System.out.println(Sts);
//                HttpSession session = request.getSession();
//                session.setAttribute("email", user);
//                session.setAttribute("status",Sts);
//                //setting session to expiry in 30 mins
//                session.setMaxInactiveInterval(30 * 60);
//                Cookie email = new Cookie("email", user);
//                Cookie Status = new Cookie("Status",Sts);
//                email.setMaxAge(30 * 60);
//                Status.setMaxAge(30*60);
//                response.addCookie(email);
//                response.addCookie(Status);
//                if ("admin".equals(Sts)) {
//                    response.sendRedirect("/paneladmin");
//                } else {
//                    response.sendRedirect("/panelsuperadmin");
//                }
        } else {
            return null;
//              
        }
    }
    
    public User getUserEmail(String email) throws SQLException, ClassNotFoundException{
        sql = "SELECT * from `admin` WHERE email='"+email+"'";
        return getUser(sql);
    }
     public User getUserNama (String nama) throws SQLException, ClassNotFoundException{
        sql = "SELECT * from `admin` WHERE email='"+nama+"'";
        return getUser(sql);
    }
    
    public void insertAdmin(User dataUser) throws ClassNotFoundException, SQLException{
        sql = "INSERT INTO `admin` (`nama`,`email`,`email`,`role`,`Ruang_id_ruang`,`password`) VALUES (?,?,1,?,?)";
        cuRegistrasi(sql);
        
    }
    public void updateAdmin(User dataUser) throws ClassNotFoundException, SQLException{
        sql = "UPDATE `admin` SET (`id_admin`,`nama`,`email`,`email`,`role`,`Ruang_id_ruang`,`password`) VALUES (?,?,1,?,?)";
        cuRegistrasi(sql);
        
    }
     public void deleteAdmin(int id) throws ClassNotFoundException, SQLException{
        sql = "DELETE FROM `admin` WHERE `id_admin`="+id;
        cuRegistrasi(sql);
    }
    
    
    public User getUser(String sql) throws SQLException, ClassNotFoundException{
        dataUser = new User();
        con = buatKoneksi();
        Statement stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        
        while(rs.next()){
            dataUser.setId_admin(rs.getInt(1));
            dataUser.setNama(rs.getString(2));
            dataUser.setEmail(rs.getString(3));
            dataUser.setRole(rs.getInt(4));
            dataUser.setId_ruang(rs.getInt(5));
            dataUser.setPassword(rs.getString(6));
                      
            
        }
        return dataUser;
    }
     private void cuRegistrasi(String sql) throws ClassNotFoundException, SQLException{
        // membuat koneksi
        con = buatKoneksi();
        st = con.createStatement();
        // execute query
        st.execute(sql);
        // menutup koneksi db
        st.close();
        con.close();
    }
}

//      
